﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;

namespace HangzhouChipProject
{
    public partial class DeviceImage : UserControl
    {
        const short CORE = 1;  //板卡
        string CruAxle1State;
        string CruAxle2State;
        string CruAxle3State;
        string CruAxle4State;
        string CruAxle5State;
        string CruAxle6State;
        string CruAxle7State;
        string CruAxle8State;
        int MC_ALARM, MC_LLimit, MC_RLimit, MC_ARRIVE;

        public DeviceImage()
        {
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            // base.OnLoad(e); 
            timer_AxleState.Start();
        }

        private void timer_AxleState_Tick(object sender, EventArgs e)
        {
            MC_ALARM = MotorsControl.ioSignalEXIMonitoring(CORE, 0);
            MC_LLimit = MotorsControl.ioSignalEXIMonitoring(CORE, 1);
            MC_RLimit = MotorsControl.ioSignalEXIMonitoring(CORE, 2);
            MC_ARRIVE = MotorsControl.ioSignalEXIMonitoring(CORE, 4);
            CruAxle1State = MotorsControl.AxisStatus(CORE, 1, 1);
            CruAxle2State = MotorsControl.AxisStatus(CORE, 2, 1);
            CruAxle3State = MotorsControl.AxisStatus(CORE, 3, 1);
            CruAxle4State = MotorsControl.AxisStatus(CORE, 4, 1);
            CruAxle5State = MotorsControl.AxisStatus(CORE, 5, 1);
            CruAxle6State = MotorsControl.AxisStatus(CORE, 6, 1);
            CruAxle7State = MotorsControl.AxisStatus(CORE, 7, 1);
            CruAxle8State = MotorsControl.AxisStatus(CORE, 8, 1);
            ShowCruAxleState(textBoxEx_AxleState1, CruAxle1State);
            ShowCruAxleState(textBoxEx_AxleState2, CruAxle2State);
            ShowCruAxleState(textBoxEx_AxleState3, CruAxle3State);
            ShowCruAxleState(textBoxEx_AxleState4, CruAxle4State);
            ShowCruAxleState(textBoxEx_AxleState5, CruAxle5State);
            ShowCruAxleState(textBoxEx_AxleState6, CruAxle6State);
            ShowCruAxleState(textBoxEx_AxleState7, CruAxle7State);
            ShowCruAxleState(textBoxEx_AxleState8, CruAxle8State);
            EXIEntranceGuard(MC_ARRIVE);
            conversionOfnumber(MC_ALARM, MC_LLimit, MC_RLimit);
            for (short i = 1; i < 9; i++)
            {
                mc.GTN_ClrSts(1, i, 8);
            }
        }
        /// <summary>
        /// 判断当前门禁的状态
        /// </summary>
        /// <param name="MC_ARRIVE"></param>
        private void EXIEntranceGuard(int MC_ARRIVE)
        {
            if ((MC_ARRIVE & 0x10) != 0 && MC_ARRIVE != -1)
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_1, Color.Green);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_1, Color.Red);
            }
            if ((MC_ARRIVE & 0x20) != 0 && MC_ARRIVE != -1)
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_2, Color.Green);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_2, Color.Red);
            }
            if ((MC_ARRIVE & 0x40) != 0 && MC_ARRIVE != -1)
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_3, Color.Green);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt1_EntranceGuard_3, Color.Red);
            }
            if ((MC_ARRIVE & 0x100) != 0 && MC_ARRIVE != -1)
            {
                InterLayer.changButtonColor(ucBtnExt_negatavicePresstext, Color.Green);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt_negatavicePresstext, Color.Red);
            }
            if ((MC_ARRIVE & 0x80) != 0 && MC_ARRIVE != -1)
            {
                InterLayer.changButtonColor(ucBtnExt_AirPresstext, Color.Green);

            }
            else
            {
                InterLayer.changButtonColor(ucBtnExt_AirPresstext, Color.Red);
            }
        }
        /// <summary>
        /// 判断轴的异常情况
        /// </summary>
        /// <param name="IoExiState"></param>
        private void conversionOfnumber(int IoExiState, int mc_llimit, int mc_rlimit)
        {
            if (IoExiState == -1) return;
            int returnState;
            int ExiALARMState;
            returnState = (IoExiState & 0x1) != 0 || (mc_llimit & 0x1) != 0 || (mc_rlimit & 0x1) != 0 ? InterLayer.changButtonColor(ucBtnExt_1axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_1axleAlam, Color.Green);
            returnState = (IoExiState & 0x2) != 0 || (mc_llimit & 0x2) != 0 || (mc_rlimit & 0x2) != 0 ? InterLayer.changButtonColor(ucBtnExt_2axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_2axleAlam, Color.Green);
            returnState = (IoExiState & 0x4) != 0 || (mc_llimit & 0x4) != 0 || (mc_rlimit & 0x4) != 0 ? InterLayer.changButtonColor(ucBtnExt_3axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_3axleAlam, Color.Green);
            returnState = (IoExiState & 0x8) != 0 || (mc_llimit & 0x8) != 0 || (mc_rlimit & 0x8) != 0 ? InterLayer.changButtonColor(ucBtnExt_4axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_4axleAlam, Color.Green);
            returnState = (IoExiState & 0x10) != 0 || (mc_llimit & 0x10) != 0 || (mc_rlimit & 0x10) != 0 ? InterLayer.changButtonColor(ucBtnExt_5axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_5axleAlam, Color.Green);
            returnState = (IoExiState & 0x20) != 0 || (mc_llimit & 0x20) != 0 || (mc_rlimit & 0x20) != 0 ? InterLayer.changButtonColor(ucBtnExt_6axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_6axleAlam, Color.Green);
            returnState = (IoExiState & 0x40) != 0 || (mc_llimit & 0x40) != 0 || (mc_rlimit & 0x40) != 0 ? InterLayer.changButtonColor(ucBtnExt_7axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_7axleAlam, Color.Green);
            returnState = (IoExiState & 0x80) != 0 || (mc_llimit & 0x80) != 0 || (mc_rlimit & 0x80) != 0 ? InterLayer.changButtonColor(ucBtnExt_8axleAlam, Color.Red) : InterLayer.changButtonColor(ucBtnExt_8axleAlam, Color.Green);




        }
        /// <summary>
        /// 显示当前轴的状态
        ///
        /// </summary>
        /// <param name="bex"></param>
        /// <param name="msg"></param>
        private void ShowCruAxleState(TextBoxEx bex, string msg)
        {

            bex.Text = msg.ToString();
        }
    }
}
